import {
  queryDbGroupList,
  addDbGroup,
  queryList,
  queryDbNameList,
  dbgroupdelete,
  queryTree,
  deleteBatch,
  queryRelDbList,
  updateDbGroup,
} from '@/services/sensitiveData/databaseGroup';

export default {
  namespace: 'databaseGroup',
  state: {
    // 数据库分组列表查询
    dataqueryDbGroupList: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },
    //选择绑定数据库的接口
    dataqueryDbNameList: [],
    dataqueryList: [],
  },

  effects: {
    // 数据库分组列表查询
    *queryDbGroupList({ payload }, { call, put }) {
      const response = yield call(queryDbGroupList, payload);
      yield put({
        type: 'savequeryDbGroupList',
        payload: response.data || {
          dataList: [],
          pageNum: 0,
          pageSize: 0,
          totalCount: 0,
        },
      });
    },
    //数据库分组新增
    *addDbGroup({ payload }, { call, put }) {
      const response = yield call(addDbGroup, payload);
      return response;
    },
    *queryList({ payload }, { call, put }) {
      const response = yield call(queryList, payload);
      yield put({
        type: 'savequeryList',
        payload: response.data ? response.data.dataList || [] : [],
      });
    },
    //选择绑定数据库的接口
    *queryDbNameList({ payload }, { call, put }) {
      const response = yield call(queryDbNameList, payload);
      yield put({
        type: 'savequeryDbNameList',
        payload: response.data ? response.data.dataList || [] : [],
      });
    },
    //数据库分组删除
    *dbgroupdelete({ payload }, { call, put }) {
      const response = yield call(dbgroupdelete, payload);
      return response;
    },
    *queryRelDbList({ payload }, { call, put }) {
      const response = yield call(queryRelDbList, payload);
      return response;
    },
    *updateDbGroup({ payload }, { call, put }) {
      const response = yield call(updateDbGroup, payload);
      return response;
    },
  },
  reducers: {
    // 数据库分组列表查询
    savequeryDbGroupList(state, { payload }) {
      return {
        ...state,
        dataqueryDbGroupList: payload,
      };
    },
    //选择绑定数据库的接口
    savequeryDbNameList(state, { payload }) {
      return {
        ...state,
        dataqueryDbNameList: payload,
      };
    },
    savequeryList(state, { payload }) {
      return {
        ...state,
        dataqueryList: payload,
      };
    },
  },
};
